package com.example.interceptor.controller.interceptor;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class userInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("当前执行到：preHandle...");
        System.out.println(request.getServletPath());   //   "/users"
        System.out.println(request.getServletContext());   //org.apache.catalina.core.ApplicationContextFacade@79e6eef9
        System.out.println(request.getHttpServletMapping());//org.apache.catalina.core.ApplicationMapping$MappingImpl@403199fc
        System.out.println(request.getSession()); // org.apache.catalina.session.StandardSessionFacade@3cd45c3c
        System.out.println(request.getCookies());//[Ljavax.servlet.http.Cookie;@52970d26
        System.out.println(request.getAuthType());//null
        System.out.println(request.getContextPath());
        System.out.println(request.getHeaderNames());//org.apache.tomcat.util.http.NamesEnumerator@16c1e1a
        System.out.println(request.getMethod());//GET
        System.out.println(request.getRemoteAddr()); //0:0:0:0:0:0:0:1
        System.out.println(request.getLocalAddr());//0:0:0:0:0:0:0:1
        System.out.println(request.getDispatcherType());//REQUEST
        System.out.println(request.getRequestURI());//users
        System.out.println("------------------------");
        return true;    //一定要返沪true，否则后面程序无法执行
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("当前执行到：postHandle...");
        System.out.println(response.getHeaderNames());// [Set-Cookie]
        System.out.println(response.getStatus()); //200
        System.out.println(response.getWriter());//org.apache.catalina.connector.CoyoteWriter@32379384
        System.out.println(response.getContentType());//null
        System.out.println("------------------------");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("当前执行到：afterCompletion...");
    }
}
